﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using HeathCareSystem;

namespace HeadvCareSystem
{
    public class DonViThuocDAO
    {
        DataProvider dp = new DataProvider();

        public List<DonViThuocDTO> LayDanhSachDonViThuoc()
        {
            DataProvider dp = new DataProvider();
            List<DonViThuocDTO> listKetQua = new List<DonViThuocDTO>();
            DonViThuocDTO dvtDTO;
            DataTable dt = new DataTable();
            string strSQL = string.Format("SELECT * FROM DonVi");
            dt = dp.ExecuteQuery(strSQL);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dvtDTO = new DonViThuocDTO();
                dvtDTO.maDonViThuoc = Int32.Parse(dt.Rows[i][0].ToString());
                dvtDTO.tenDonViThuoc = dt.Rows[i][1].ToString();
                listKetQua.Add(dvtDTO);
            }
            return listKetQua;
        }

        
        public static bool DonViThuocMoi(DonViThuocDTO dv)
        {
            DataProvider dp = new DataProvider();
            string strSQL = string.Format("INSERT INTO DonVi (TenDonViThuoc, MaDonViThuoc)" +
                                     "VALUES (N'{0}',{1})", dv.tenDonViThuoc, dv.maDonViThuoc);
            return dp.ExecuteNonQuery(strSQL);
        }


        // Lấy toàn bộ Loại dvuốc
        public static List<DonViThuocDTO> GetAll()
        {
            String strSql = "SELECT * FROM DonVi";
            DataProvider dp = new DataProvider();
            DataTable dtb = dp.ExecuteQuery(strSql);

            List<DonViThuocDTO> lstdv = new List<DonViThuocDTO>();

            foreach (DataRow dr in dtb.Rows)
            {
                DonViThuocDTO dvDTO = new DonViThuocDTO();
                //dvuocDTO.madvuoc = Convert int(dr["Madvuoc"].ToString());
                //dvuocDTO.tendvuoc = dr["Tendvuoc"].ToString();
                //dvuocDTO.maDonVi = dr["MaDonVi"].ToString();
                lstdv.Add(dvDTO);
            }

            return lstdv;
        }

        // Xóa
        public static bool Delete(string strMadv)
        {
            DataProvider a = new DataProvider();
            String strSql = "DELETE FROM dvUOC WHERE Madvuoc = '" + strMadv + "'";

            return a.ExecuteNonQuery(strSql);
        }

        // Update
        public static bool Update(DonViThuocDTO dv)
        {
            DataProvider dp = new DataProvider();
            String strSql = "UPDATE DonVi SET TenDonViThuoc = N'" + dv.tenDonViThuoc + "', MaDonViThuocuoc= " + dv.maDonViThuoc + "  WHERE MaDonViThuoc = '" + dv.maDonViThuoc + "'";

            return dp.ExecuteNonQuery(strSql);
        }

        // Check Tên
        public static bool Check_Name(string strTendv)
        {
            DataProvider dp = new DataProvider();
            String strSql = "SELECT * FROM DonVi WHERE TenDonViThuoc = N'" + strTendv + "'";
            DataTable dtb = dp.ExecuteQuery(strSql);

            if (dtb.Rows.Count > 0)
                return false;
            return true;
        }
    }

}